/*
    Open Manager, an open source file manager for the Android system
    Copyright (C) 2009, 2010, 2011  Joe Berria <nexesdevelopment@gmail.com>

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.elcom.player;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;

import com.elcom.player.R;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class EventHandler implements OnClickListener {
	private final Context 		context;
	private final FileManager 	file_mg;
	private int 				color = Color.WHITE;
	private TableRow 			delegate;
	private ArrayList<String> data_source;
	
	public EventHandler(Context context, final FileManager manager, int type) {
		this.context = context;
		file_mg 	 = manager;
		String temp = Globals.getSdcardPath();
		if (type == Globals.MANAGER_TYPE_ALL){
			data_source  = new ArrayList<String>(file_mg.getLastOpenedDir());
		}else if (type == Globals.MANAGER_TYPE_VIDEO){
			data_source = new ArrayList<String>();
			
			File file;
			for (int ii=0; ii< file_mg.getLastOpenedDir().size();ii++){
				 file = new File(temp + "/" + file_mg.getLastOpenedDir().get(ii));
				 if(file.isFile()  ) {
						String ext = file.toString();
						String sub_ext = ext.substring(ext.lastIndexOf(".") + 1);
						if(Arrays.asList(Globals.supportedVideoFileFormats).contains(sub_ext.toLowerCase()))
						{
							data_source.add(file_mg.getLastOpenedDir().get(ii));
						} 
					}
			}
		}else if (type == Globals.MANAGER_TYPE_SUBTITLE){
			data_source = new ArrayList<String>();
			//String temp = file_mg.getCurrentDir();
			File file;
			for (int ii=0; ii< file_mg.getLastOpenedDir().size();ii++){
				 file = new File(temp + "/" + file_mg.getLastOpenedDir().get(ii));
				 if(file.isFile()  ) {
						String ext = file.toString();
						String sub_ext = ext.substring(ext.lastIndexOf(".") + 1);
						if(Arrays.asList(Globals.supportedSubtileFileFormats).contains(sub_ext.toLowerCase()))
						{
							data_source.add(file_mg.getLastOpenedDir().get(ii));
						} 
					}
			}
		}else if (type == Globals.MANAGER_TYPE_SUBFONT){
			data_source = new ArrayList<String>();
			//String temp = file_mg.getCurrentDir();
			File file;
			for (int ii=0; ii< file_mg.getLastOpenedDir().size();ii++){
				 file = new File(temp + "/" + file_mg.getLastOpenedDir().get(ii));
				 if(file.isFile()  ) {
						String ext = file.toString();
						String sub_ext = ext.substring(ext.lastIndexOf(".") + 1);
						if(Arrays.asList(Globals.supportedFontFileType).contains(sub_ext.toLowerCase()))
						{
							data_source.add(file_mg.getLastOpenedDir().get(ii));
						} 
					}
			}
		}
	}
	@Override
	public void onClick(View arg0) {
		// TODO Auto-generated method stub
		
	}
	public String getData(int position) {

		if(position > data_source.size() - 1 || position < 0)
			return null;

		return data_source.get(position);
	}
	private static class ViewHolder {
		TextView topView;
		TextView bottomView;
		ImageView icon;
		ImageView mSelect;	//multi-select check mark icon
	}
	public class TableRow extends ArrayAdapter<String> {
		private final int KB = 1024;
		private final int MB = KB * KB;
		private final int GB = MB * KB;
		private String display_size;
		private ArrayList<Integer> positions;

		public TableRow() {
			super(context, R.layout.tablerow, data_source);
		}
		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder holder;
			int num_items = 0;
			String temp = file_mg.getCurrentDir();
			File file = new File(temp + "/" + data_source.get(position));
			String[] list = file.list();

			if(list != null)
				num_items = list.length;

			if(convertView == null) {
				LayoutInflater inflater = (LayoutInflater) context.
				getSystemService(Context.LAYOUT_INFLATER_SERVICE);

				convertView = inflater.inflate(R.layout.tablerow, parent, false);

				holder = new ViewHolder();
				holder.topView = (TextView)convertView.findViewById(R.id.top_view);
				holder.bottomView = (TextView)convertView.findViewById(R.id.bottom_view);
				holder.icon = (ImageView)convertView.findViewById(R.id.row_image);
				holder.mSelect = (ImageView)convertView.findViewById(R.id.multiselect_icon);

				convertView.setTag(holder);

			} else {
				holder = (ViewHolder)convertView.getTag();
			}

			if (positions != null && positions.contains(position))
				holder.mSelect.setVisibility(ImageView.VISIBLE);
			else
				holder.mSelect.setVisibility(ImageView.GONE);

			holder.topView.setTextColor(color);
			holder.bottomView.setTextColor(color);

			if(file.isDirectory()) {
				holder.icon.setImageResource(R.drawable.folder);

			} else if(file.isFile()  ) {
				String ext = file.toString();
				String sub_ext = ext.substring(ext.lastIndexOf(".") + 1);
				/*This series of else if statements will determine which icon is displayed*/
				if (Arrays.asList(Globals.supportedAudioFileFormats).contains(sub_ext.toLowerCase()))
				{
					holder.icon.setImageResource(R.drawable.music);
				}  else if(Arrays.asList(Globals.supportedFontFileType).contains(sub_ext.toLowerCase()))
				{
					holder.icon.setImageResource(R.drawable.ttf);
				}  else if(Arrays.asList(Globals.supportedVideoFileFormats).contains(sub_ext.toLowerCase()))
				{
					holder.icon.setImageResource(R.drawable.movies);
				} 
			}

			if(file.isFile()) {
				double size = file.length();
				if (size > GB)
					display_size = String.format("%.2f Gb ", (double)size / GB);
				else if (size < GB && size > MB)
					display_size = String.format("%.2f Mb ", (double)size / MB);
				else if (size < MB && size > KB)
					display_size = String.format("%.2f Kb ", (double)size/ KB);
				else
					display_size = String.format("%.2f bytes ", (double)size);

				if(file.isHidden())
					holder.bottomView.setText("(hidden) | " + display_size );
				else
					holder.bottomView.setText(display_size );
			} else {
				if(file.isHidden())
					holder.bottomView.setText("(hidden) | " + num_items + " items ");
				else
					holder.bottomView.setText(num_items + " items ");
			}

			holder.topView.setText(file.getName());

			return convertView;

		}
	}
	
}
